﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Hxj.Data;
using Utility.Model;
using Utility.Json;
using Utility;
using System.Data;

namespace MsgCenter.Web.Handlers
{
    /// <summary>
    /// Handler_UserGroup 的摘要说明
    /// </summary>
    public class Handler_UserGroup : BaseUI.BaseHandler
    {
        public void GetGroupAllList()
        {
            WhereClip wcp = new WhereClip();
            wcp = wcp.And(Sys_Group._.IsDelete == false);
            wcp = wcp.And(Sys_Group._.CreateUser == base.Account);
            using (DataTable dt = BLL.BLLServiceHelper.GetGroupService().GetGroupList(wcp))
            {
                JsonJqGridData grid = new JsonJqGridData();
                grid.page = 0;
                grid.records = int.MaxValue;
                grid.total = int.MaxValue;
                grid.rows = dt;
                string strResult = grid.ToJsonString();
                base.Response.Write(strResult);
            }
        }


        #region 获取用户列表分页
        /// <summary>
        /// 获取用户列表
        /// </summary>
        public void GetGroupList()
        {
            WhereClip wcp = new WhereClip();
            wcp = wcp.And(Sys_Group._.IsDelete == false);
            wcp = wcp.And(Sys_Group._.CreateUser == base.Account);
            var pageConditon = GetPageConditon(wcp);             
            pageConditon.ComplexSearchCondition = wcp;
            JsonJqGridData gridList = BLL.BLLServiceHelper.GetGroupService().GetGroupList(pageConditon);
            string strResult = gridList.ToJsonString();
            base.Response.Write(strResult);
        }
        #endregion

        #region 删除用户
        /// <summary>
        /// 删除
        /// </summary>
        public void Delete()
        {
            string strID = ParamsOfRequest.Request("UserID");
            try
            {
                BLL.BLLServiceHelper.GetGroupService().Delete(strID);
                base.GetSuccessJson();
            }
            catch (Exception ex)
            {
                base.GetFailureJson(ex.Message);
            }
        }
        #endregion

        public void SetUserGroup()
        {
            string strUserIDS = ParamsOfRequest.Request("UserID");
            string strGroupID = ParamsOfRequest.Request("GroupID");
            try
            {
                BLL.BLLServiceHelper.GetGroupService().SetUserGroup(strUserIDS,strGroupID);
                base.GetSuccessJson();
            }
            catch (Exception ex)
            {
                base.GetFailureJson(ex.Message);
            }

        }

        public void GetGroupUser()
        {
            string strGroupID = ParamsOfRequest.Request("GroupID");
            base.Response.Write(BLL.BLLServiceHelper.GetGroupService().GetGroupUser(strGroupID).ToJson());
        }

        public void GetGroupUserList()
        {
            string strGroupID = ParamsOfRequest.Request("GroupID");
            using (DataTable dt = BLL.BLLServiceHelper.GetGroupService().GetGroupUser(strGroupID))
            {
                JsonJqGridData grid = new JsonJqGridData();
                grid.page = 0;
                grid.records = int.MaxValue;
                grid.total = int.MaxValue;
                grid.rows = dt;
                string strResult = grid.ToJsonString();
                base.Response.Write(strResult);
            }
        }

        public void DeleteGroupUser()
        {
            string strUserIDS = ParamsOfRequest.Request("UserID");
            string strGroupID = ParamsOfRequest.Request("GroupID");
            try
            {
                BLL.BLLServiceHelper.GetGroupService().DeleteGroupUser(strUserIDS, strGroupID);
                base.GetSuccessJson();
            }
            catch (Exception ex)
            {
                base.GetFailureJson(ex.Message);
            }
        }

    }
}